//
//  BuddyManager.m
//  CandyCrushClone
//
//  Created by Petar on 2/10/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "BuddyManager.h"
#import "Globals.h"

@implementation BuddyManager
{
    CommunicationManager *_communicationManager;
    __weak SmartFox2XClient *_smartFoxClient;

}


- (instancetype)initWithCommunicationManager:(CommunicationManager *)communicationManager andSFSClient:(SmartFox2XClient *)sfsClient
{
    self = [super init];
    if (self) {
        
        _smartFoxClient = sfsClient;
        _communicationManager = communicationManager;
        
    }
    return self;
}


-(void)removeUserFromBuddyList:(NSString *)username
{
    
    NSArray *buddies = [[_smartFoxClient buddyManager] buddyList];
    BOOL shouldRemoveBuddy = NO;
    
    // Search for Buddy in my BuddyList with this name
    for (SFSUser *buddy in buddies) {
        if ( [buddy.name isEqual:username] ) {
            shouldRemoveBuddy = YES;
            break;
        }
    }
    
    // Send RemoveBuddyRequest only if you have Buddy with this name
    if ( shouldRemoveBuddy == YES ) {
        [_smartFoxClient send:[RemoveBuddyRequest requestWithBuddyName:username]];
    } else {
        NSLog(@"You do NOT have buddy with name: %@",username);
    }
    
}


-(void)onBuddyAdd:(SFSBuddyEvent *)evt
{
    
    SFSBuddy *buddy = [evt.params objectForKey:@"buddy"];
    
    NSString *message = [NSString stringWithFormat:@"%@ is added in your BuddyList.",buddy.name];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invitation" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    
}


// Shows Message passed between Buddies
-(void)onBuddyMessage:(SFSBuddyEvent *)evt
{
    
    SFSBuddy *buddy;
    SFSBuddy *sender;
    
    BOOL isItMe = [[evt.params objectForKey:@"isItMe"] getBoolValue];
    NSString *message = [[evt.params objectForKey:@"message"] getStringValue];
    NSString *buddyName;
    
    sender = [evt.params objectForKey:@"buddy"];
    
    if ( isItMe == YES ) {
        
        SFSObject *playerData = [[evt.params objectForKey:@"data"] getSFSObjectValue];
        buddyName = [playerData getUtfString:@"recipient"];
        buddy = [[_smartFoxClient buddyManager] getBuddyByName:buddyName];
        
    } else {
        
        buddy = sender;
        buddyName = @"";
        
    }
    
    if ( buddy != nil ) {
        
        // Store the message according to the sender's name
        NSString *toMessage = [NSString stringWithFormat:@"To %@: %@", buddyName, message];
        NSString *fromMessage = [NSString stringWithFormat:@"From %@: %@", buddy.name, message];
        
        if ( isItMe == YES ) {
            message = toMessage;
        } else {
            message = fromMessage;
        }
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Buddy message" message:message delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        
    }
    
}

@end
